package com.oocl.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.oocl.activemq.MessageProducerFactory;
import com.oocl.dto.MUserRegDTO;
import com.oocl.exception.MyException;
import com.oocl.pojo.Muser;
import com.oocl.service.MuserService;
import com.oocl.service.impl.MuserServiceImpl;
import com.oocl.util.Base64Util;
import com.oocl.util.FileUtil;
import com.oocl.util.PropertiesFactory;
import com.oocl.util.RequestUtil;


public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory(20480,new File("D:/temp"));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(1024000);
		List<FileItem> list = null;
		Muser  muser = new Muser();
		muser.setRegistDate(new Date());
		try {
		list = upload.parseRequest(request);
			String fileName = "";
			byte[] portrait =null;
			byte[] idCard =null;
			
			for (FileItem item : list) {
				if (item.isFormField()) {
					if ("name".equals(item.getFieldName())) {
						muser.setMname(item.getString());
					} else if ("password".equals(item.getFieldName())) {
						muser.setPwd(item.getString());
					} else if ("icnum".equals(item.getFieldName())) {
						muser.setIcnum(item.getString());
						fileName = item.getString() + ".png";
					} else if ("addr".equals(item.getFieldName())) {
						muser.setMaddr(item.getString());
					}
				} else {
					InputStream in = item.getInputStream();
					if("portrait".equals(item.getFieldName())){
						portrait = new  byte[in.available()];
						in.read(portrait);
					}else{
						idCard = new byte[in.available()];
						in.read(idCard);
					}
					in.close();
				}
			}
			String portraitPath = PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("portraitfile")
					+ File.separator + fileName;
			String idCardPath = PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("iccardfile")
					+ File.separator + fileName;
		
			FileUtil.toFile(portrait, portraitPath);
			FileUtil.toFile(idCard, idCardPath);
			muser.setPortrait(fileName);
			MuserService mSer = new MuserServiceImpl();
			String token = UUID.randomUUID().toString();
			muser.setToken(token);
			mSer.addMuser(muser);
			MUserRegDTO mu = new MUserRegDTO();
			mu.setIcNum(muser.getIcnum());
			mu.setAddr(muser.getMaddr());
			mu.setIcPic(Base64Util.wrapBase64Forsavepic(idCard));
			mu.setName(muser.getMname());
			mu.setPortrait(Base64Util.wrapBase64Forsavepic(portrait));
			mu.setToken(token);
			MessageProducerFactory.newInstance().createMsgProducer().send(RequestUtil.wrapToJson(mu));
			response.sendRedirect("login.jsp");
		} catch (MyException e) {
			response.sendRedirect("exception.jsp");
		} catch (FileUploadException e) {
			response.sendRedirect("exception.jsp");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
